package com.example.imageloaderdemo.view;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.GridView;

import com.example.imageloaderdemo.R;
import com.example.imageloaderdemo.adapter.ImageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wdl
 */
public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener {

    private GridView gridView;
    private ImageAdapter imageAdapter;
    private List<String> urlList;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUrlList();
        gridView = findViewById(R.id.gv_image);
        imageAdapter = new ImageAdapter(this, urlList);
        gridView.setAdapter(imageAdapter);
        gridView.setOnScrollListener(this);
    }

    private void initUrlList() {
        urlList = new ArrayList<>();
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0161a1cd87506776d4.jpg");
        urlList.add("https://p3.ssl.qhimgs1.com/sdr/400__/t01bae40022bd9f9ef3.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t01d22ea66cc2cec4de.jpg");
        urlList.add("https://p2.ssl.qhimgs1.com/sdr/400__/t010419bf8a421593dc.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0126b8e63bbeeb155e.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0161a1cd87506776d4.jpg");
        urlList.add("https://p3.ssl.qhimgs1.com/sdr/400__/t01bae40022bd9f9ef3.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t01d22ea66cc2cec4de.jpg");
        urlList.add("https://p2.ssl.qhimgs1.com/sdr/400__/t010419bf8a421593dc.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0126b8e63bbeeb155e.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0161a1cd87506776d4.jpg");
        urlList.add("https://p3.ssl.qhimgs1.com/sdr/400__/t01bae40022bd9f9ef3.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t01d22ea66cc2cec4de.jpg");
        urlList.add("https://p2.ssl.qhimgs1.com/sdr/400__/t010419bf8a421593dc.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0126b8e63bbeeb155e.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0161a1cd87506776d4.jpg");
        urlList.add("https://p3.ssl.qhimgs1.com/sdr/400__/t01bae40022bd9f9ef3.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t01d22ea66cc2cec4de.jpg");
        urlList.add("https://p2.ssl.qhimgs1.com/sdr/400__/t010419bf8a421593dc.jpg");
        urlList.add("https://p0.ssl.qhimgs1.com/sdr/400__/t0126b8e63bbeeb155e.jpg");
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            imageAdapter.setIfGridIdle(true);
            imageAdapter.notifyDataSetChanged();
        } else {
            imageAdapter.setIfGridIdle(false);
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }
}
